//input stream, extremly fast but not stl-compliant (yet?)
//$Id: fast_stream.h 97 2009-08-26 15:11:42Z Oleg.Bulychov $
#pragma once
#ifndef FAST_STREAM_H
#define FAST_STREAM_H

#include <string>
//__________________________________________________________________________________
//__________________________________________________________________________________
class fast_istream
{
    char const* text_;
    size_t      pos_;
    size_t      len_;
public:
    explicit fast_istream(char const* str, size_t cch): text_(str), pos_(0),
        len_(cch)
    {}

    fast_istream& operator >> (std::string& s)
    {
        for (;pos_ < len_; ++pos_)
        {
            char c = text_[pos_];
            if (c > ' ')
            {
                size_t from = pos_;
                for (++pos_; pos_ < len_; ++pos_)
                {
                    c = text_[pos_];
                    if (c <= ' ')
                        break;
                }
                s.assign(&text_[from], pos_ - from);
                ++pos_;
                return *this;
            }
        }
        s.clear();
        return *this;
    }

    fast_istream& operator >> (int& i)
    {
        for (;pos_ < len_; ++pos_)
        {
            char c = text_[pos_];
            if (c > ' ')
            {
                bool negative = false;
                if ('-'== c)
                {
                    negative = true;
                    ++pos_;
                }
                int number = 0;
                for (; pos_ < len_; ++pos_)
                {
                    c = text_[pos_];
                    if ('0' <= c && c <= '9')
                    {
                        int digval = c - '0';
                        number = number * 10 + digval;
                    }
                    else
                        break;
                }
                i = negative ? -number : number;
                ++pos_;
                return *this;
            }
        }
        i = 0;
        return *this;
    }
};
//__________________________________________________________________________________

#endif
//EOF!
